package org.UserHouse.Dao.Imp;

import org.UserHouse.Dao.OrderDao;
import org.UserHouse.Utils.Dbutils;
import org.UserHouse.entity.Order;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author bin
 * @date 2024-12-13
 */
public class OrderDaoImp implements OrderDao {


    @Override
    public List<Order> selectPageOrder(Order order, int page, int limit) {
        List<Order> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select *");
        sql.append(" from (");
        sql.append(" select ");
        sql.append(" t_orders.o_id,u_user.u_name,h_item.h_name,t_orders.o_status,t_orders.o_date,t_orders.h_price,t_orders.o_size, u_address.a_address, ");
        sql.append(" row_number() over (order by t_orders.o_date asc) as rownumber");
        sql.append(" from t_orders");
        sql.append(" left join u_address on t_orders.u_id = u_address.u_id");
        sql.append(" left join h_item on t_orders.h_id = h_item.h_id");
        sql.append(" left join u_user on t_orders.u_id = u_user.u_id");
        sql.append(" where t_orders.o_id is not null");
        if (order != null && order.getHname() != null) {
            sql.append(" and h_name like ?");
            params.add("%" + order.getHname() + "%");
        }
        sql.append(" ) as t2");
        sql.append(" where rownumber between ? and ?;");
        int startPage = (page - 1) * limit + 1;
        params.add(startPage);
        int endPage = (page * limit);
        params.add(endPage);

        Connection conn = Dbutils.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        try {
            pst = conn.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                pst.setObject(i + 1, params.get(i));
            }
            rst = pst.executeQuery();

            while (rst.next()) {
                Order o = new Order();
                String Oid = rst.getString(1);
                String Uname = rst.getString(2);
                String Hname = rst.getString(3);
                String Ostatus = rst.getString(4);
                Date Odate = rst.getDate(5);
                Double Hprice = rst.getDouble(6);
                int Osize = rst.getInt(7);
                String Aaddress = rst.getString(8);

                o.setOid(Oid);
                o.setUname(Uname);
                o.setHname(Hname);
                o.setOstatus(Ostatus);
                o.setOdate(Odate);
                o.setHprice(Hprice);
                o.setOsize(Osize);
                o.setAaddress(Aaddress);

                list.add(o);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Long selectPageCount(Order order) {
        long r = 0;
        List<Order> list = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select count(*)  from t_orders");
        sql.append(" left join h_item on t_orders.h_id = h_item.h_id");
        sql.append(" left join u_user on t_orders.u_id = u_user.u_id");
        sql.append(" left join u_address on t_orders.u_id = u_address.u_id");
        sql.append(" where t_orders.o_id is not null");
        ;
        if (order != null && order.getHname() != null) {
            sql.append(" and h_name like ?");
            params.add("%" + order.getHname() + "%");
        }
        Connection conn = Dbutils.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        try {
            pst = conn.prepareStatement(sql.toString());
            for (int i = 0; i < params.size(); i++) {
                pst.setObject(i + 1, params.get(i));
            }
            rst = pst.executeQuery();
            if (rst.next()) {
                r = rst.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return r;
    }

    @Override
    public int AddOrder(Order order) {
        return 0;
    }

    @Override
    public int deleteOrder(int Oid) {
        int n = 0;
        String sql = "delete  t_orders  where o_id = ?";
        Connection conn = Dbutils.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, Oid);
            n = pst.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return n;
    }

    @Override
    public int updateOrder(Order order) {
        return 0;
    }
}
